package com.yyg.boot.web;

import com.yyg.boot.entity.db1.Goods;
import com.yyg.boot.entity.db2.User;
import com.yyg.boot.service.GoodsService;
import com.yyg.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/3
 * @Description Description
 */
@RestController
public class TransactionController {

    @Autowired
    private UserService userService;

    @Autowired
    private GoodsService goodsService;

    @Transactional(value = "jtaTransactionManager", rollbackFor = Exception.class)
    @GetMapping("/add")
    public ResponseEntity addTest() {
        Goods goods = new Goods();
        goods.setId(44L);
        goods.setName("iPhone se");
        goodsService.addGoods(goods);

        User user = new User();
        user.setSex("男");
        user.setUsername("一一哥");
        user.setAddress("上海");
        user.setBirthday(new Date());
        userService.addUser(user);

        return ResponseEntity.ok("添加成功");
    }

    @Transactional(value = "jtaTransactionManager", rollbackFor = Exception.class)
    @GetMapping("/add2")
    public ResponseEntity addTest2() {
        Goods goods = new Goods();
        goods.setId(44L);
        goods.setName("iPhone se");
        goodsService.addGoods(goods);

        User user = new User();
        user.setSex("男");
        user.setUsername("一一哥");
        user.setAddress("上海");
        user.setBirthday(new Date());
        userService.addUser(user);

        int i=10/0;

        return ResponseEntity.ok("添加成功");
    }

}
